#ifndef __PARSEHTTPMESSAGE_H__
#define __PARSEHTTPMESSAGE_H__

#include "common.h"

class HttpRequest
{
public:
~HttpRequest();
HttpRequest();
    
    const std::string& getParam();
    const std::string& getMethod();
    const std::string& getBody();
    bool isLegalMethod();
    bool isGetMethod();
    bool isPostMethod();

    void parseUrl();
    void setUrlToPath();
    void setPath(const std::string& path);
    std::string getPath();
    void addHomePage();
    void setCgi();
    bool getCgi();
    void setFileSize(size_t filesize);
    size_t getFileSize();
    std::string getPathSuffix();

    void parseMsg(const char* msg, size_t len);

private:
    std::string req_line_;//请求行
    std::vector<std::string> req_header_;//请求头
    std::string req_blank_;//空行
    std::string req_body_;//请求正文

private:
    std::string method_;//请求方法
    std::string url_;
    std::string version_;

    std::map<std::string, std::string> header_map_;
    size_t content_length_;

    std::string path_;
    std::string param_;
    bool iscgi_;
    size_t filesize_;
};

class connection;
class HttpResponse
{
public:
    HttpResponse();
    ~HttpResponse();
    void makeRespLine(size_t code);
    void addHeader(const std::string& header);
    //void addBody(const char* body);
    void sendResponse(connection* conn);

    typedef struct{
        size_t size_;
        char buf[];
    } RespBodyType;
    RespBodyType* resp_body_;

private:
    std::string resp_line_;
    std::vector<std::string> resp_header_;
    std::string resp_blank_;
    //std::string resp_body_;

};


class HttpEndPoint
{
public:
    HttpEndPoint(connection* conn);
    ~HttpEndPoint();

    void setConnData(connection* connptr);
    void parseRequest(const char* msg, size_t msglen);
    void makeResponse();
    void sendResponse();
private:
    void exeCgi();
    void exeNoCgi();

    void makeRespLine(size_t code);
    void makeRespHeader();
    void makeRespBody();
    //将resp加入到server发送缓存中。
private:
    HttpRequest req_;
    HttpResponse resp_;

    void* conndata_;
};

#endif